/**
 * 
 */
package com.yidu.menuProject.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.yidu.menuProject.action.vo.MealVo;
import com.yidu.menuProject.common.Pages;
import com.yidu.menuProject.common.ResultMap;
import com.yidu.menuProject.commont.IndustrySMS;
import com.yidu.menuProject.commont.Tools;
import com.yidu.menuProject.model.EhyManagerModel;
import com.yidu.menuProject.model.MealModel;
import com.yidu.menuProject.service.EhyManagerService;
import com.yidu.menuProject.service.MealService;

/**
 *@author liuping
 *@date 2018年7月18日 下午8:05:10
 *@version 1.0
 */
@RequestMapping("meal")
@Controller
public class MealAction {
	
	@Resource
	private MealService mealService;
	@Resource
	private EhyManagerService managerService;
	
	
	@RequestMapping("adds")
	public String adds(
			@RequestParam(value = "pic", required = false)
			MultipartFile file,
				MealVo vo,
				EhyManagerModel man,
				HttpServletRequest request) throws IllegalStateException, IOException{
		//使用UUID给图片重命名，并去掉四个“-”
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		//获取文件的扩展名
		String ext = FilenameUtils.getExtension(file.getOriginalFilename());
		//设置图片上传路径
		String url = request.getSession().getServletContext().getRealPath("/upload");
		System.out.println(url);
		//以绝对路径保存重名命后的图片
		file.transferTo(new File(url+"/"+name + "." + ext));
		//把图片存储路径保存到数据库
		vo.setMealLogo("upload/"+name + "." + ext);
		System.out.println("增加进来");
		vo.setMealId(Tools.getRandomString());
		vo.setIsva(1);
		mealService.insert(vo);
		man.setManId(Tools.getRandomString());
		man.setIsva(2);
		man.setMealId(vo.getMealId());
		managerService.insert(man);
		return "redirect:/pages/index.html" ;
	}
	
	
	
	//获取验证码
	/**
	 * 短信验证
	 */
	@RequestMapping("/foodYZM")
	@ResponseBody 	//将返回的数据转换为json
	public Object foodYZM(String mealPhone ,HttpServletRequest request){
		System.out.println("电话号码"+mealPhone);
		//先定义一个空的String
		String str = "";
		//定义随机
		Random rd=new Random();
		//循环取4个随机数
		for(int i=0;i<4;i++){
			int k=rd.nextInt(9);
			str+=k;
		}
		//使用色是sesstion保存以便注册时判断短信验证码是否填写正确
		request.getSession().setAttribute("setfood", str);
		//短信内容
		String con = "【馋了科技】您的验证码为"+str+"，请于5分钟内正确输入，如非本人操作，请忽略此短信。";
		//验证码通知短信接口
		IndustrySMS.execute(con, mealPhone);
		return str;
	}
	
	@RequestMapping("/selectAll")
	@ResponseBody
	public ResultMap<List<MealModel>> selectAll(Pages page,@RequestParam("limit") int limit){
		System.out.println("========"+limit);
		System.out.println(page);
		List<MealModel> list = mealService.selectAll(page);
		Integer count = mealService.selectCount(page);
		page.setTotalRecord(count);
		return new ResultMap<List<MealModel>>("",list,0,count);
	}
	
	/**
	 * 审核查询所有
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/select")
	@ResponseBody
	public ResultMap<List<MealModel>> select(Pages page,@RequestParam("limit") int limit){
		System.out.println("========"+limit);
		System.out.println(page);
		List<MealModel> list = mealService.select(page);
		
		Integer count = mealService.selectCounts(page);
		page.setTotalRecord(count);
		
		return new ResultMap<List<MealModel>>("",list,0,count);
	}
	
	/**
	 * 删除
	 * @param vo
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(MealVo vo){
		System.out.println("进入删除========");
		mealService.deleteByPrimaryKey(vo);
		return "ok";
	}
	/**
	 * 删除 状态为2删除为3
	 * @param vo
	 * @param mealId
	 * @return
	 */
	@RequestMapping("/del")
	@ResponseBody
	public String deletes(MealVo vo,String mealId){
		System.out.println("进入修改删除=========");
		vo.setMealId(mealId);
		System.out.println("======"+mealId);
		int rows = mealService.delete(vo);
		if(rows > 0){
			return "ok";
		}else{
			return "no";
		}
	}
	/**
	 * 判断审核是否通过
	 * @param vo
	 * @return
	 */
	@RequestMapping("/updateVet")
	@ResponseBody
	public MealModel updateVet(MealVo vo) {
		System.out.println("进入审核============");
		vo.setIsva(2);
		mealService.updateVet(vo);
		MealModel msg = new MealModel();
		return msg;
	}
	
	
	/**
	 * 增加
	 * @param vo
	 * @return
	 */
	@RequestMapping("/add")
	public String insert(
			@RequestParam(value = "pic",required = false) 
			MultipartFile file,
			MealVo vo,
			HttpServletRequest request) throws IllegalStateException, IOException{
		System.out.println("增加进入=======");
		//使用UUID给图片重命名，并去掉四个“-”
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		//获取文件的扩展名
		String ext = FilenameUtils.getExtension(file.getOriginalFilename());
		//设置图片上传路径
		String url = request.getSession().getServletContext().getRealPath("/upload");
		System.out.println(url);
		//以绝对路径保存重名命后的图片
		file.transferTo(new File(url+"/"+name + "." + ext));
		//把图片存储路径保存到数据库
		vo.setMealLogo("upload/"+name + "." + ext);
		System.out.println("增加进来");

		vo.setMealId(Tools.getRandomString());
		mealService.insertSelective(vo);
		/*mv.setViewName("redirect:/pages/meal/meal.html");*/
		return "redirect:/pages/meal/meal.html";
	}
	
	/**
	 * 修改
	 * @param file
	 * @param vo
	 * @param request
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("/update")
	@ResponseBody
	public ModelAndView Update(
			@RequestParam(value = "pic",required = false) 
			MultipartFile file,
			MealVo vo,
			HttpServletRequest request) throws IllegalStateException, IOException{
		System.out.println("修改进入=======");
		ModelAndView mv = new ModelAndView();
		//将图片保存到upload文件夹,并且返回一个新的文件地址
		String path = Tools.getRealPath(request, file);
		vo.setMealLogo(path);
		mealService.updateByPrimaryKeySelective(vo);
		mv.setViewName("redirect:/pages/meal/meal.html");
		return mv;
	}
	
	@RequestMapping("/findById")
	@ResponseBody
	public MealModel findById(String mealId){
		return mealService.selectByPrimaryKey(mealId);
	}
	
	
}
